package service.impl;

import dao.IPermissionDao;
import domain.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.IPermissionService;
import service.IRoleService;

import java.util.List;

/**
 * @author Freedy
 * @date 2020/12/26 10:34
 */
@Service
public class IPermissionServiceImpl implements IPermissionService {
    @Autowired
    private IPermissionDao permission;
    @Autowired
    private IRoleService roleService;
    @Override
    public List<Permission> findAll() throws Exception {
        return permission.findAll();
    }

    @Override
    public void save(Permission permission) throws Exception {
        this.permission.save(permission);
    }

    @Override
    public void deletePermission(String permissionId) throws Exception {
        permission.deletePermissionFromRolePermission(permissionId);
        permission.deletePermission(permissionId);
    }

    @Override
    public List<Permission> findAllWithOutItself(String id) throws Exception {
        List<Permission> all = findAll();
        List<Permission> permissions = roleService.findById(id).getPermissions();
        for (Permission a : all) {
            for (Permission p : permissions) {
                if (a.getId().equals(p.getId())){
                    all.remove(a);
                }
            }
        }
        return all;
    }

    @Override
    public Permission findById(String permissionId) throws Exception {
        return permission.findById(permissionId);
    }
}
